package com.yunji.system.service.engine;

import com.google.common.io.Files;
import com.yunji.common.base.RuoYiConfig;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.model.domain.CaptchaPicsDO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.common.engine
 * @Title: CaptchaPicsRuleEngine
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-11-19 17:17:16      V1.0        HuaAo       新建类
 */
@Component
public class CaptchaPicsHandle {


    @Value("${aj.captcha.jigsaw}")
    private String captchaPath;

    public void handleCaptcha(CaptchaPicsDO captchaDO) {
        Integer status = captchaDO.getStatus();
        String path = captchaDO.getPath();
        String picsName = captchaDO.getPicsName();
        if (Objects.equals(LogicCode.NO, status)) {
            try {
                Files.copy(new File(path.replace("/profile/upload", RuoYiConfig.getUploadPath())), new File(captchaPath + "/original/" + picsName + ".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            // 停用 背景图库剔除
            File file = new File(captchaPath + "/original/" + picsName + ".png");
            file.delete();
        }

    }

}